package string_question.leecode.test32;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/7 8:49
 */
public class Solution2 {

    // 方法二 ： 借用左右指针的性值
    public int longestValidParentheses(String s) {
        if (s == null || s.length() < 2) return 0;
        char[] chars = s.toCharArray();
        int ans = 0;
        for (int i = 0; i < chars.length; i++) {
            int count = 0;
            for (int j = i; j < chars.length; j++) {
                if (chars[j] == '(') {
                    count++;
                } else {
                    count--;
                }
                if (count < 0) {
                    break;
                }
                if (count == 0) {
                    ans = Math.max(ans, j - i + 1);
                }
            }
        }
        return ans;
    }
}
